package com.g8.pethospitallearnbackend.controller;

import com.g8.pethospitallearnbackend.entity.*;
import com.g8.pethospitallearnbackend.exception.CustomException;
import com.g8.pethospitallearnbackend.mapper.DiseaseMapper;
import com.g8.pethospitallearnbackend.service.CaseService;
import com.g8.pethospitallearnbackend.service.CasefieldsService;
import com.g8.pethospitallearnbackend.service.DiseaseService;
import com.g8.pethospitallearnbackend.utils.responseUtil.ResponseStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/case")
public class CaseController {
    @Autowired
    private DiseaseService diseaseService;
    @Autowired
    private CaseService caseService;
    @Autowired
    private CasefieldsService casefieldsService;

    @GetMapping("/")
    public String noVariable(){
        throw new CustomException(ResponseStatusEnum.BAD_REQUEST,"diseaseName不能为空");
    }

    /**
     * 返回具体病名对应的病例信息
     * @param diseaseName
     * @return
     */
    @GetMapping("/{diseaseName}")
    public List<CaseIntro> getIntroductionByDiseaseName(@PathVariable String diseaseName){
        if(diseaseName.length()>100){
            throw new CustomException(ResponseStatusEnum.BAD_REQUEST,"diseaseName长度超出限制");
        }
        String diseaseId=diseaseService.getDiseaseIdByName(diseaseName);
        if(diseaseId==null){
            throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST,"该病名不存在");
        }
        List<CaseIntro> cases=caseService.getCaseIntroByDiseaseId(diseaseId);
        if(cases==null|| cases.isEmpty()||null==cases.get(0)){
            throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST,"该病名下暂无病例信息");
        }
        return cases;
    }

    /**
     * 返回指定病例中指定字段的英文名，中文名，字段值
     * @param caseId
     * @param modules
     * @return
     */
    @GetMapping("/{caseId}/module/{modules}")
    public List<ModuleInfo> getModuleInfoToCaseId(@PathVariable String caseId,@PathVariable String modules){
        if(caseId.length()!=8) throw new CustomException(ResponseStatusEnum.BAD_REQUEST,caseId+"不合法");
        if(modules.length()>100) throw new CustomException(ResponseStatusEnum.BAD_REQUEST,modules+"不合法");
        List<ModuleInfo> moduleInfos=caseService.getModuleInfoToCaseId(caseId,modules);
        if(moduleInfos==null||moduleInfos.isEmpty())
            throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST,"未查询到相关信息");
        return moduleInfos;
    }

    @GetMapping("/backend-allcases")
    public List<Case> getAllCases(){
        List<Case> cases=caseService.getAllCases();
        if(cases==null||cases.isEmpty()){
            throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST,"无病例信息");
        }
        return cases;
    }

    @GetMapping("/case-fields")
    public List<Casefields> getAllFields(){
        List<Casefields> casefields=casefieldsService.getAllFields();
        if(casefields==null||casefields.isEmpty()){
            throw new CustomException(ResponseStatusEnum.DATA_NOT_EXIST,"无病例模板字段");
        }
        return casefields;
    }

    /**
     * 修改一个病例
     * @param caseId
     * @param caseinfo
     * @return
     */
    @PutMapping("/{caseId}")
    public Case modifyACase(@PathVariable String caseId, @RequestBody CaseReceived caseinfo){
        if(!caseId.matches("c\\d{7}")) throw new CustomException(ResponseStatusEnum.BAD_REQUEST,caseId+"不合法");
        Case handled=caseService.modifyACase(caseId,caseinfo);
        if(handled==null) throw new CustomException(ResponseStatusEnum.BAD_REQUEST,"修改失败");
        return handled;
    }

    /**
     * 添加一个病例，和修改类似
     * @param caseinfo
     * @return
     */
    @PostMapping("")
    public String addACase(@RequestBody CaseReceived caseinfo){
        String newId=caseService.addACase(caseinfo);
        if(newId==null) throw new CustomException(ResponseStatusEnum.BAD_REQUEST,"新增失败");
        return newId;
    }

    /**
     * 批量删除病例
     * @param caseIds
     * @return
     */
    @DeleteMapping("/{caseIds}")
    public List<String> deleteCases(@PathVariable String caseIds){
        List<String> deleted=caseService.deleteCases(caseIds);
        return deleted;
    }


}
